package controllers;

import helpers.CrawlerHelper;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RewriterServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String article = "";
		String rewrite = "";
		
		request.setAttribute("article", article);
		request.setAttribute("rewrite", rewrite);
		try {
			request.getRequestDispatcher("/pages/rewriter.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		
		String article = (String) request.getParameter("article");
		
		String rewrite = "";
		
		if (article != null && !article.isEmpty()) {
			rewrite = CrawlerHelper.rewriteArticle(article);
		}
		
		request.setAttribute("article", article);
		request.setAttribute("rewrite", rewrite);
		try {
			request.getRequestDispatcher("/pages/rewriter.jsp").forward(request, response);
		} catch (ServletException e) {
			//e.printStackTrace();
		}
	}
}
